<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/Leads for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Leads\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel,
Leads\Form\CreateForm,
Leads\Model\Entity\Person,
Zend\Db\Adapter\Adapter;
use Leads\Model\LeadsTable;

class LeadsController extends AbstractActionController
{
	protected $_LeadsTable;
	
	public function getLeadsTable()
	{
		if (!$this->_LeadsTable) {
			$sm = $this->getServiceLocator();
			$this->_LeadsTable = $sm->get('Leads\Model\LeadsTable');
		}
		return $this->_LeadsTable;
	}
	
	
    public function indexAction()
    {
    	
    	$uri = $this->getRequest()->getUri();
    	$scheme = $uri->getScheme();
    	$host = $uri->getHost();
    	$base = sprintf('%s://%s', $scheme, $host);
    	
    	return new ViewModel(array(
            'leads' => $this->getLeadsTable()->fetchAll(),
    		/*'contact' => $this->getLeadsTable()->fetchAll(),*/
        ));
		echo 'Sembarang';
    }

    public function fooAction()
    {
        // This shows the :controller and :action parameters in default route
        // are working when you browse to /leads/leads/foo
        return array();
    }
    
    public function cobaAction()
    {
    	$view = new ViewModel();
    	$view->setTemplate('leads/leads/coba');
    	return $view;
    }
    
    public function createcobaAction()
    {
    	$form = new CreateForm();
    	$form->get('submit')->setValue('Save');
    	$request = $this->getRequest();
    	
    	if($request->isPost())
    	{
    		$person = new Person();
    		$form->setInputFilter($person->getInputFilter());
    		$form->setData($request->getPost());
    			
    		if($form->isValid())
    		{
    			
    			$person->exchangeArray($form->getData());
    			$this->getLeadsTables()->saveLead($person);
    		}
    		
    		
    	
    	}return array('form' => $form);
    }
    /*
    public function addAction() {
    	$request = $this->getRequest();
    	$response = $this->getResponse();
    	if ($request->isPost()) {
    		$new_note = new \StickyNotes\Model\Entity\StickyNote();
    		if (!$note_id = $this->getStickyNotesTable()->saveStickyNote($new_note))
    			$response->setContent(\Zend\Json\Json::encode(array('response' => false)));
    		else {
    			$response->setContent(\Zend\Json\Json::encode(array('response' => true, 'new_note_id' => $note_id)));
    		}
    	}
    	return $response;
    }
    
    public function removeAction() {
    	$request = $this->getRequest();
    	$response = $this->getResponse();
    	if ($request->isPost()) {
    		$post_data = $request->getPost();
    		$note_id = $post_data['id'];
    		if (!$this->getStickyNotesTable()->removeStickyNote($note_id))
    			$response->setContent(\Zend\Json\Json::encode(array('response' => false)));
    		else {
    			$response->setContent(\Zend\Json\Json::encode(array('response' => true)));
    		}
    	}
    	return $response;
    }
    public function updateAction(){
    	// update post
    	$request = $this->getRequest();
    	$response = $this->getResponse();
    	if ($request->isPost()) {
    		$post_data = $request->getPost();
    		$note_id = $post_data['id'];
    		$note_content = $post_data['content'];
    		$stickynote = $this->getStickyNotesTable()->getStickyNote($note_id);
    		$stickynote->setNote($note_content);
    		if (!$this->getStickyNotesTable()->saveStickyNote($stickynote))
    			$response->setContent(\Zend\Json\Json::encode(array('response' => false)));
    		else {
    			$response->setContent(\Zend\Json\Json::encode(array('response' => true)));
    		}
    	}
    	return $response;
    } */
}
